package com.chatplus.application.web.verification.impl;

import com.chatplus.application.common.logging.SouthernQuietLogger;
import com.chatplus.application.common.logging.SouthernQuietLoggerFactory;
import com.chatplus.application.common.util.email.MailUtils;
import com.chatplus.application.web.VerificationConfig;
import com.chatplus.application.web.verification.PlusCaptchaService;
import org.redisson.api.RedissonClient;
import org.springframework.stereotype.Service;

@Service(value = "emailCaptchaService")
public class EmailCaptchaServiceImpl extends PlusCaptchaService {
    static final SouthernQuietLogger LOGGER = SouthernQuietLoggerFactory.getLogger(EmailCaptchaServiceImpl.class);

    protected final VerificationConfig.MailCaptchaProperties mailCaptchaProperties;

    public EmailCaptchaServiceImpl(VerificationConfig.MailCaptchaProperties mailCaptchaProperties, RedissonClient redissonClient) {
        super(redissonClient);
        this.mailCaptchaProperties = mailCaptchaProperties;
    }

    @Override
    public boolean sendNotice(String receiver, String noticeMsg) {
        return send(receiver, "岸上吧AI通知", noticeMsg);
    }

    @Override
    public boolean doSendCaptcha(String email, String captcha) {
        return send(email, "岸上吧AI验证码", "您本次验证码为：" + captcha + "，有效性为" + getCaptchaValidateTime().toMinutes() + "分钟，请尽快填写。");
    }

    private boolean send(String email, String subject, String msg) {
        if (Boolean.TRUE.equals(mailCaptchaProperties.getEnabledMock())) {
            return true;
        }
        try {
            MailUtils.sendText(email, subject, msg);
            return true;
        } catch (Exception e) {
            LOGGER.message("验证码短信发送异常").exception(e).error();
        }
        return false;
    }
}
